package com.zhuss.food.processor;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.zhuss.core.processor.EnumConfig;
import com.zhuss.enums.ConfigTypeEnum;
import com.zhuss.enums.OrderStatus;
import com.zhuss.enums.ProductType;
import com.zhuss.food.db.dao.ext.OrderDao;
import com.zhuss.food.db.entity.Order;
import com.zhuss.util.DateUtil;


@Component("orderCheck")
public class OrderCheck {
	
	@Autowired
	private OrderDao orderDao;
	
	@Autowired
	private EnumConfig enumConfig;
	
	public Boolean checkOverCancelTime(Long orderId, Integer area) {
		Date overTime = enumConfig.getTime(ConfigTypeEnum.点炒截止取消时间.tag(), area);
		Order order = orderDao.selectByPrimaryKey(orderId);
		Date createTime = order.getCreateTime(); // 下单的时间
		Date createDate = DateUtil.toDateOnly(createTime); // 下单的日期
		Date now = new Date(); // 当前时间
		Date nowDate = DateUtil.toDateOnly(now); // 当前的日期
		Integer type = order.getType();  // 订单的类型
		// 如果是点炒订单， 而且 取消截止时间已过，  当前日期大于下单日期即过往日的订单，  不支持取消
		if (ProductType.点炒服务.sameKey(type) && 
				(overTime.getTime() < now.getTime() || createDate.getTime() < nowDate.getTime())) {
			return true;
		}
		
		// 如果是外卖打包订单， 超过了 外卖打包 点餐时间区间， 当前日期大于下单日期   即过往的订单， 不支持取消
		overTime = enumConfig.getTime(ConfigTypeEnum.外卖截止时间.tag(), area);
		if (ProductType.特价外卖.sameKey(type) && 
				(overTime.getTime() < now.getTime() || createDate.getTime() < nowDate.getTime())) {
			return true;
		}
		return false;
	}
	
	public Boolean checkBelongUser(Long orderId, Long userId) {
		Order order = orderDao.selectByPrimaryKey(orderId);
		if (userId.equals(order.getCreatorId())) {
			return true;
		}
		return false;
	}

	public boolean checkCancel(Long orderId) {
		Order order = orderDao.selectByPrimaryKey(orderId);
		if (OrderStatus.未取餐.sameKey(order.getStatus()) || OrderStatus.未接单.sameKey(order.getStatus())) {
			return true;
		}
		return false;
	}
	
	public boolean checkFetch(Long orderId) {
		Order order = orderDao.selectByPrimaryKey(orderId);
		if (OrderStatus.未取餐.sameKey(order.getStatus())) {
			return true;
		}
		return false;
	}
	
	public boolean checkReceive(Long orderId) {
		Order order = orderDao.selectByPrimaryKey(orderId);
		if (OrderStatus.未接单.sameKey(order.getStatus())) {
			return true;
		}
		return false;
	}
	
	public boolean checkTopic(Long orderId) {
		Order order = orderDao.selectByPrimaryKey(orderId);
		if (OrderStatus.已取餐.sameKey(order.getStatus())) {
			return true;
		}
		return false;
	}

}
